﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage2_1 : Stage
    {
        protected List<SpotLight> _spotLightList = new List<SpotLight>();

        public Stage2_1(Game game) : base(game) { }

        float GROUND_ALPHA = 0.2f;

        public override void Restart()
        {
            base.Restart();
            foreach (SpotLight spotLight in _spotLightList)
                spotLight.Restart();
        }

        public override void InitializeScenes()
        {
            base.InitializeScenes();

            Animation title = new Animation("images/title/stageTitle", new Rectangle(142, 268, 98, 42), new Point(98, 42));
            title.Scale = 1.2f;

            Transition tra = new DefauldTransition(1, 0, 0, 60);
            tra.Register(this);
            tra = new TitleTransition(title, new Vector2(430, 150), 0, 1, 80, 220);//460, 391
            tra.Register(this);

                Animation wallBack = new Animation("images/stages/stage2.1/stage2.1_1");
                //wallBack.LayerDepth = 0.9f;
                _wallBackGround.Add(wallBack);
            
            _wallBackGroungPos = new Vector2(-340, 80);


            wallBack = new Animation("images/stages/stage2.1/stage2.1_2");
                //wallBack.LayerDepth = 0.9f;
            _wallBackGroundOther.Add(wallBack);
            _wallBackGroungOtherPos = new Vector2(1894, 52);
            _scenceBound = new Rectangle(430, 20,
                   2540,
                  550);

            GroundObject ground =
            StateFactory.CreateGround(this, -52, 339, 202, 280, 0f); ground.Alpha = 0;
            ground =
            StateFactory.CreateGround(this, 378, 454, 657, 50, 0f); ground.Alpha = 0;
            StateFactory.CreatePortal("Stage2_1_Portal_1", this, 245, 379, "stage2", "Stage2_Protal_2");
            ground =
            StateFactory.CreateGround(this, 845, 404, 451, 50, 0f); ground.Alpha = GROUND_ALPHA;
            ground =
            StateFactory.CreateGround(this, 1314, 454, 661, 50, 0f); ground.Alpha = GROUND_ALPHA;
            SpotLight _spotLight =
            StateFactory.CreateSpotLight(this, 380, 0, 480, 30, -90); _spotLightList.Add(_spotLight);
            Switch gameSwitch =
            StateFactory.CreateSwitch(this, 1414, 264); 
            _spotLight =
            StateFactory.CreateSpotLight(this, 1160, 0, 480, 30, -90); _spotLightList.Add(_spotLight); gameSwitch.AddTarget(_spotLight);
            ground =
            StateFactory.CreateGround(this, 1254, 370, 100, 19, 0f); ground.Alpha = GROUND_ALPHA;
            ground =
            StateFactory.CreateGround(this, 1414, 299, 100, 19, 0f); ground.Alpha = GROUND_ALPHA;
            ground =
            StateFactory.CreateGround(this, 1849, 404, 550, 50, 0f); ground.Alpha = GROUND_ALPHA;
            _spotLight =
            StateFactory.CreateSpotLight(this, 1495, 0, 480, 30, -90); _spotLight.IsLight = false; gameSwitch.AddTarget(_spotLight); _spotLightList.Add(_spotLight);
            ground =
            StateFactory.CreateGround(this, 1590, 256, 100, 20, 0f); ground.Alpha = GROUND_ALPHA;
            GameKey key =
            StateFactory.CreateKey("Stage2_1_Key_S1", this, 1599, 228, "Stage2_1_door_1"); key.Alpha = 0.3f;
            GameDoor door =
            StateFactory.CreateDoor("Stage2_1_door_S1", this, 1828, 328, "Stage2_1_door_1"); door.Alpha = 0.3f;
            ground =
            StateFactory.CreateGround(this, 2252, 454, 502, 50, 0f); ground.Alpha = 0;
            ground =
            StateFactory.CreateGround(this, 2625, 318, 244, 323, 0f); ground.Alpha = 0;
            StateFactory.CreatePortal("Stage2_1_Portal_2", this, 2397, 379, "stage3", "Stage3_Protal_1");
            _spotLight =
            StateFactory.CreateSpotLight(this, 2295, 0, 480, 30, -90); _spotLightList.Add(_spotLight);



            Font = ResourceManager.LoadSpriteFont("SpriteFont1");
            MusicManager.AddMusic("Music/doorlock", "doorlock", 0.001f, false);

            _song = MusicManager.LoadSong("Music/cave");
            if (GameClass._musicString == "Music/cave")
            { }
            else
            {
                GameClass._musicString = "Music/cave";
                MediaPlayer.Play(_song);
            }
            MediaPlayer.IsRepeating = true;
           // MediaPlayer.Volume = 1f;

            _bloom = _gameClass._BloomComponent;
            _bloom.ScenrDraw += DrawBackGrounds;
            _bloom.ScenrDraw += DrawWalls;

            _startProtal = new ProtalCommand("stage2_1", "Stage2_1_Portal_1");

        }


        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {
            GraphicsDevice.Clear(Color.Black);
            _bloom.BeginDraw();
            GraphicsDevice.Clear(Color.Black);
            foreach (SpotLight spotLight in _spotLightList)
                spotLight.OnDraw(spriteBatch, camera.Transform);
            _bloom.Draw();


            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);

            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);

            spriteBatch.End();

            //spriteBatch.Begin();
            // spriteBatch.DrawString(Font, "" + test, new Vector2(50, 50), Color.Black);
            // spriteBatch.End();

        }

        public override void Update(int delta)
        {
            base.Update(delta);

            //a.Update(delta);
            foreach (SpotLight spotLight in _spotLightList)
                spotLight.Update(delta);
            //mic.Update();
            // MusicManager.FadeInMusic("first");


        }

        public override Stage Clone()
        {
            Stage2_1 stage = new Stage2_1(Game);
            stage.InitializeScenes();
            return stage;
        }

        public override void Dispose()
        {
            base.Dispose();
            _bloom.ScenrDraw -= DrawBackGrounds;
            _bloom.ScenrDraw -= DrawWalls;
        }
    }
}
